<?
//Initiate Authenticate
SecurityRedirect ();
//initiate Vars
define("PRIMARY_KEY","nokwit");
$KEY[]         = array("PRIMARY_KEY"=>PRIMARY_KEY);
$FileName      = "ais_transaction_receipt_summary.php";
$FileTemplate  = "ais_transaction_receipt_summary_list.html";
$TableName     = "ais_receipt";
$SubHeaderTitle= "Transaction Receipt - Summary Receipt";
$PageSize      = 10 ;
$HTMLGridList  = "ReceiptGrid";
//************ FORM ACTION **************
        if (GetParam("Action","")=="Delete") {
                DeleteRecord ("txtCheck",$PageSize);
        }

$QueryGet  = GetQueryString("All", array('PageNum'));

//Initiate Database Table For Search;
$SQLLedger = "SELECT ledger FROM ais_history_ledger ORDER BY ledger DESC LIMIT 1";
$FLedger = (strlen(GetParam("ledger",""))>0)?GetParam("ledger",""):$DBConnection->dbc->get_var($SQLLedger);
$FCode = trim(GetParam("receiptCodeSearch",""));


//$FSkada = GetParam("skadaSearch","");
//$FDept = GetParam("deptSearch","");
//******* DATABASE FORM QUERY ***********
//$SQL = "SELECT $TableName.*, sum(jumlah) as sumjumlah FROM `$TableName` ";


$SQL = "SELECT $TableName.*,sum($TableName.jumlah) as sumjumlah, IF (tb_tk.nobkt<>'',tb_tk.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,'')) as aslref FROM `$TableName` ";
$SQL .= "LEFT JOIN ais_memorial tb_tk ON $TableName.nokwit=tb_tk.noref AND tb_tk.urut=999 ";
$SQL .= "LEFT JOIN ais_memorial_z LZ ON $TableName.nokwit=LZ.noref AND LZ.urut=999 ";



//$SQLCount = "SELECT count(*) FROM `$TableName` ";
/*
$SQL = "SELECT @nokwit:=$TableName.nokwit as nokwit, $TableName.curr, $TableName.debnam, $TableName.rate, $TableName.tglkwit, (SELECT sum(a.jumlah) FROM $TableName a WHERE nokwit=@nokwit GROUP BY nokwit) as sumjumlah, IF (tb_tk.nobkt<>'',tb_tk.nobkt,IF(LZ.nobkt<>'',LZ.nobkt,'')) as aslref FROM `$TableName` ";
$SQL .= "LEFT OUTER JOIN ais_memorial tb_tk ON $TableName.nokwit=tb_tk.noref AND tb_tk.urut=1 AND $TableName.urut=1 ";
$SQL .= "LEFT OUTER JOIN ais_memorial_z LZ ON $TableName.nokwit=LZ.noref AND LZ.urut=1 AND $TableName.urut=1 ";
*/


/*
$SQL = "SELECT @nokwit:=$TableName.nokwit as nokwit, $TableName.curr, $TableName.debnam, $TableName.rate, $TableName.tglkwit, @tktbno:=(SELECT nobkt FROM ais_memorial WHERE noref=@nokwit AND urut=1 limit 1) as tktbno, @ztktbno:=(SELECT nobkt FROM ais_memorial_z WHERE noref=@nokwit AND urut=1 limit 1) as ztktbno, (SELECT sum(a.jumlah) FROM $TableName a WHERE nokwit=@nokwit) as sumjumlah, IF (@tktbno<>'',@tktbno,IF(@ztktbno<>'',@ztktbno,'')) as aslref FROM `$TableName` ";
//$SQL .= "LEFT JOIN ais_memorial tb_tk ON $TableName.nokwit=tb_tk.noref AND tb_tk.urut=999 ";
//$SQL .= "LEFT JOIN ais_memorial_z LZ ON $TableName.nokwit=LZ.noref AND LZ.urut=999 ";
*/

$WHERE .= "$TableName.urut=1 AND ";
$WHERE .= "$TableName.ktrasl=".GetOfficeID()." ";


//Retrieve Parameter from search
if ($FLedger) {
        $CustomWhere = " $TableName.ledger like '".$FLedger."'";
        if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
        else $WHERE = $CustomWhere;
}

if ($FCode) {
        $CustomWhere = " nokwit like '".$FCode."%'";
        if (strlen($WHERE)) $WHERE .= " AND ".$CustomWhere;
        else $WHERE = $CustomWhere;
}

if (strlen($WHERE)) {
        $SQL      = $SQL."WHERE ".$WHERE;
        $SQLCount = $SQLCount."WHERE ".$WHERE;
} else $SQL = $SQL;
$SQL = $SQL." GROUP BY nokwit ";
$SQL = $SQL." ORDER BY tglkwit, nokwit DESC ";

//print $SQL;
//$SQLCount = $SQLCount." AND urut=1";

//Select Customer
//$SQLDept = "SELECT * FROM tbldept";

//******* SHOW FORM CONTENT *********
$PageNum = 1;
/*
$RecCnt = $DBConnection->dbc->get_var($SQLCount) ;
*/
//$RecCnt = mysql_num rows($SQL);
if (!$RecCnt) $RecCnt=0;

if (GetParam("PageNum","")) $PageNum  = GetParam("PageNum","");

//Default value
if (!isset($_GET)) $_GET=&$HTTP_GET_VARS ;
if (isset($_GET['PageNum'])) {
  $PageNum = $_GET['PageNum'] ;
} else {
    $PageNum = 1 ;
}

//Default value
if (isset($_GET['RecCnt'])) {
  $RecCnt = intval($_GET['RecCnt']) ;
} else {
    $RecCnt = -1 ;
}
$SQLLedger = "SELECT * FROM ais_history_ledger ORDER BY ledger DESC";
$TBS = new clsTinyButStrong ;
$TBS->LoadTemplate($MOD_TEMPLATE_DIR."/".$FileTemplate);
$TBS->MergeBlock("bln",$KEY);
$TBS->MergeBlock("ledgerblk",$DBMysql,$SQLLedger);
//if ($FCode)
$RecCnt = $TBS->MergeBlock("blg",$DBMysql,$SQL,$PageSize,$PageNum,$RecCnt);
//$TBS->MergeBlock("deptblk",$DBMysql,$SQLDept);
$TBS->MergeNavigationBar('nv',array('size'=>10,'pos'=>'centred'),$PageNum,$RecCnt,$PageSize) ;
$TBS->Show();


function DeleteRecord ($sequence_name,$PageSize) {
        global $DBConnection,$FileName,$TableName;
        for ($x=1;$x<=$PageSize;$x++) {
                if (GetParam($sequence_name.$x,""))        {
                        $DBConnection->Delete("$TableName",$sequence_name.$x);
            }
        }
        header("Location: $FileName");
        exit;
}

?>